package com.epoint.mobileoa.ui.mail;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.bizlogic.mobileoa.GetWDMailNumsTask;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.LogHelp;

/**
 * @author liyc
 * @time 2012-2-1 下午7:33:04
 * @annotation
 */
public class MailMain extends SuperView {

	private GridView gv;

	public static final int GetWDMailNums = 1;//获取未读邮件数量

	public static int mailNums = 0;
	private String[] titles;
	@Override
	public void initControls() {
		titles = new String[] {"收件箱("+mailNums+")", "发件箱", "草稿箱", "写邮件"};
		gv = (GridView) findViewById(R.id.mail_gd);
		gv.setOnItemClickListener(this);
		gv.setAdapter(getAdapter(titles));
	}

	public SimpleAdapter getAdapter(String[] titles){
		int[] imgs = new int[] { R.drawable.mail_inbox, R.drawable.mail_outbox,
				R.drawable.mail_draft, R.drawable.img_writemail};
		List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < imgs.length; i++) {
			Map<String, Object> mapItem = new HashMap<String, Object>();
			mapItem.put("img", imgs[i]);
			mapItem.put("title", titles[i]);
			listItems.add(mapItem);
		}
		
		int iconlayout;
		
		if(getBackgroundStr().equals("white")){
			iconlayout = R.layout.gvicon;
		}
		else{
			iconlayout = R.layout.gvicon_white;
		}
		
		SimpleAdapter sa = new SimpleAdapter(this, listItems, iconlayout,
				new String[] { "img", "title" }, new int[] { R.id.gvicon_img,
			R.id.gvicon_title });
		return sa;
	}

	
	@Override
	protected void onResume() {
		super.onResume();
		Task task = new GetWDMailNumsTask(this,new HashMap());
		MainService.addTask(task);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		switch (position) {
		case 0:
			Intent it_sjx = new Intent(this, MailBoxReceiveView.class);
			startActivity(it_sjx);
			break;
		case 1:
			Intent it_fjx = new Intent(this, MailBoxSendView.class);
			startActivity(it_fjx);
			break;
		case 2:
			Intent it_cgx = new Intent(this, DraftListView.class);
			startActivity(it_cgx);
			break;
		case 3:
			Intent it_xyj = new Intent(this, MailSendView.class);
			it_xyj.putExtra("pagetype", 0);
			startActivity(it_xyj);
			break;
		}
	}

	@Override
	public void refresh(Object... param) {
		if(param[0].toString().equals(MainService.NORMAL)){
				mailNums = Integer.parseInt(param[1].toString());
				titles[0] = "收件箱("+mailNums+")";
				gv.setAdapter(getAdapter(titles));
				try{
					MainService.lastallmailnums = Integer.parseInt(param[1].toString());
				}catch(Exception e){
					LogHelp.LogE("邮件数量更新异常");
			}
		}
	}

	@Override
	public int getLayoutInt() {
		return R.layout.mailmain;
	}

	@Override
	public String setTitle() {
		return getString(R.string.gwyj);
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}

}
